import Vue from 'vue'
import VueRouter from 'vue-router'
import LoginView from "@/views/LoginView.vue"
import AdminView from "@/views/AdminView.vue"
import store from '../store'
import Index from "@/views/Index.vue";
import UserView from "@/views/UserView.vue";



Vue.use(VueRouter)

const routes = [
  {
    path: '/',
    redirect: '/login'
  },
  {
    path: '/login',
    name: 'login',
    component: LoginView,
  },
  {
    path: '/admin',
    name: 'admin',
    component: AdminView,
    children:[
      {
        path:'',
        name:'index',
        component:Index
      },
      {
        path:'user',
        name:'user',
        component:UserView
      }
    ]
  }
]

const router = new VueRouter({
  mode: 'history',
  base: process.env.BASE_URL,
  routes
})

//路由守卫
router.beforeEach((to, from, next) => {
  console.log("to",to)
  if (to.path === '/login') {
    next()
  } else {
    //取出登陆后的身份信息
    if(!store.state.user){
      next({ path: '/login'})
    }
  }
  next()
})
export default router